package com.gba.mark.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gba.mark.group.entity.Group;
import com.gba.mark.group.service.IGroupService;
import com.gba.mark.user.auth.utils.AuthUtils;
import com.gba.mark.user.entity.permission.UserRole;
import com.gba.mark.user.entity.user.User;
import com.gba.mark.user.service.permission.IUserRoleService;
import com.gba.mark.user.service.user.IUserService;
import com.gba.mark.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IUserService userServiceImpl;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IGroupService groupService;

    /**
     *
     * @author:chenyitong
     * @date:21.7.19
     */
    @PostMapping("/saveUserRole")

    public ResponseUtil<Boolean> saveUserRole(Authentication authentication,
                                              @RequestParam String userName,
                                              @RequestParam String groupName) {
        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", userName));
        int userId = user.getId();

        String username = AuthUtils.getUsername(authentication);
        User userAd = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", username));
        int adminId = userAd.getId();
        Group group = groupService.getOne(new QueryWrapper<Group>().eq("name", groupName).eq("admin_id", adminId));
        int groupId = group.getId();

        return new ResponseUtil<>(HttpStatus.OK, "", userRoleService.saveUserRole(userId, groupId));

    }

    @DeleteMapping("/removeUserRole")
    public ResponseUtil<Boolean> removeUserRole(Authentication authentication,
                                                   @RequestParam String userName,
                                                   @RequestParam String groupName){
        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", userName));
        int userId = user.getId();

        String username = AuthUtils.getUsername(authentication);
        User userAd = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", username));
        int adminId = userAd.getId();
        Group group = groupService.getOne(new QueryWrapper<Group>().eq("name", groupName).eq("admin_id", adminId));
        int groupId = group.getId();

        return new ResponseUtil<>(HttpStatus.OK, "", userRoleService.removeUserRole(userId, groupId));
    }


    @GetMapping("/listUserRole")
    public ResponseUtil<List<UserRole>> listUserRole(Authentication authentication,
                                                     @RequestParam String groupName){
        String username = AuthUtils.getUsername(authentication);
        User user = userServiceImpl.getOne(new QueryWrapper<User>().eq("username", username));
        int adminId = user.getId();
        Group group = groupService.getOne(new QueryWrapper<Group>().eq("name", groupName).eq("admin_id", adminId));
        int groupId = group.getId();
        return new ResponseUtil<>(HttpStatus.OK,"", userRoleService.listUserRole(groupId));
    }

}
